#-*- coding: utf-8 -*-
#!/usr/bin/env python
# @author: lipeng
# @time: 2019/01/31
# description: general tools

import os
import json
import sys
reload(sys); sys.setdefaultencoding("utf-8")

# calc external_bounding_box of two overlapped box
def external_rect(box1, box2):
    # box: x, y, w, h
    x1, y1, w1, h1 = box1
    x2, y2, w2, h2 = box2
    x_l_u = min(x1, x2)
    y_l_u = min(y1, y2)
    x_r_d = max(x1 + w1, x2 + w2)
    y_r_d = max(y1 + h1, y2 + h2)
    x, y, w, h = x_l_u, y_l_u, x_r_d - x_l_u, y_r_d - y_l_u
    return [x, y, w, h]

# calc_iou
def calc_iou(box1, box2):
    # box: x, y, w, h
    x1, y1, w1, h1 = box1
    x2, y2, w2, h2 = box2
    area1 = w1 * h1
    area2 = w2 * h2

    intersect_x1 = max(x1, x2)
    intersect_y1 = max(y1, y2)
    intersect_x2 = min(x1+w1, x2+w2)
    intersect_y2 = min(y1+h1, y2+h2)
    intersect_w = max(0, intersect_x2 - intersect_x1 + 1)
    intersect_h = max(0, intersect_y2 - intersect_y1 + 1)
    area = intersect_w * intersect_h
    iou = area * 1.0 / (area1 + area2 - area + 1e-6)

    return iou

# load json
def load_json(json_file):
    json_info = None
    with open(json_file, 'r') as f:
        json_info = json.load(f)
    return json_info

def str2box(str_box):
    return map(int, str_box.split(','))

def box2str(int_box):
    return ','.join(map(str, int_box))

# pad img
def pad_img(img, box, up_ratio, down_ratio, left_ratio, right_ratio):
    x, y, w, h = box
    img_h, img_w = img.shape[:2]
    # pad the box img
    # x_ = x - int(left_ratio * w) if x - int(left_ratio * w) > 0  else 0
    x_ = max(0, x - int(left_ratio * w))
    # y_ = y - int(up_ratio * h) if y - int(up_ratio * h) > 0 else 0
    y_ = max(0, y - int(up_ratio * h))
    # w_ = int((1 + left_ratio + right_ratio) * w) if int((1 + left_ratio + right_ratio) * w) < img_w - x_ else img_w - x_
    w_ = min(img_w - w_, int((1 + left_ratio + right_ratio) * h))
    # h_ = int((1 + up_ratio + down_ratio) * h) if int((1 + up_ratio + down_ratio) * h) < img_h - y_ else img_h - y_
    h_ = min(img_h - y_, int((1 + up_datio + down_ratio) * h))
    box_ = (x_, y_, w_, h_)
    return img[y_:y_+h_, x_:x_+w_], box_

# print recog_res on image
def show_recog_res(img_path, color_id, label, type_dict, save_res_path):
    img = cv2.imread(img_path)
    color_name = 'pred:' + type_dict[str(color_id)]
    label_name = 'label' + type_dict[str(label)]
    h, w = img.shape[:2]
    ## cv_img to PIL_img
    img_PIL = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    font_size = w // 15
    font = ImageFont.truetype('/home/lipeng/utils/Font/simhei.ttf', font_size)
    pred_fillColor=(255, 0, 0)  # RGB
    label_fillColor = (0, 255, 0)
    p_label = (5, 5)
    p_color = (5, 5 + font_size)
    if not isinstance(color_name, unicode):
        type_name.decode('utf8')
    if not isinstance(label_name, unicode):
        label_name.decode('utf8')
    draw = ImageDraw.Draw(img_PIL)
    draw.text(p_color, color_name, font=font, fill=pred_fillColor)
    draw.text(p_label, label_name, font=font, fill=label_fillColor)
    # PIL_img to cv_img
    img = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR)
    save_img_path = os.path.join(save_res_path, img_path.split('/')[-1])
    cv2.imwrite(save_img_path, img)


if __name__=="__main__":
    pass
